//
//  WSSelectSexController.m
//  BaseProject
//
//  Created by karong on 2023/6/12.
//

#import "WSSelectSexController.h"
#import "CTMediator+StartAction.h"
#import "BaseTabBarController.h"
#import "WSUserInfoRequest.h"
#import "UIButton+ImageAlign.h"

@interface WSSelectSexController ()

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *sexArray;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (nonatomic, assign) NSInteger index;

@end

@implementation WSSelectSexController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.index = 3;
    for (UIButton * btn in self.sexArray) {
        [btn setButtonImageTitleStyle:(WSButtonImageTitleStyleTop) padding:21];
    }
}

- (void)setUserSexRequest:(NSInteger)value {
    [WSToast loading];
    WeakSelf
    [WSUserInfoRequest editcharonTV_UserInfoWithField:@"sex" integerValue:value success:^{
        [WSToast dismiss];
        appUserCenter.sex = value;
        [weakSelf showMainViewControllerWithObject:nil];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
    }];
}

- (void)showMainViewControllerWithObject:(id)object {    
    BaseNavigationController * nav = [[BaseNavigationController alloc]initWithRootViewController:[BaseTabBarController new]];
    nav.navigationBarHidden = YES;
    typedef void (^Animation)(void);
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        [UIView setAnimationsEnabled:oldState];
    };
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:animation
                    completion:^(BOOL finished) {
        if (finished) {
        }
    }
    ];
}
- (IBAction)selectSexAction:(UIButton *)sender {
    self.index = sender.tag;
}

- (IBAction)confirmAction:(id)sender {
    if (self.index == 3) {
        [WSToast simple:@"請選擇你的性別"];
    }
    else {
        [self setUserSexRequest:self.index];
    }
}

- (IBAction)skipAction:(id)sender {
    [self setUserSexRequest:3];
}

- (void)setIndex:(NSInteger)index {
    _index = index;
    for (int i = 0; i < self.sexArray.count; i++) {
        UIButton * btn = self.sexArray[i];
        btn.selected = index == btn.tag;
    }
}

@end
